﻿using System;
using GDIT.Base.PageBaseHelper;
using GDIT.BLL;
using GDIT.Model;

namespace GDIT.page.manage.user
{
    public partial class role : System.Web.UI.Page
    {

        private BRoles roles = null;
        private Mtb_Roles roleInfo = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (string.IsNullOrEmpty(Session["UserName"].ToString()))
                {
                    PageBase.MessageBoxShowAndGo("检查到你还没有登录，请先登录，谢谢！", "~/page/manage/login.aspx", Page);
                    return;
                }
                if (!IsPostBack)
                {
                    BindRoleData();
                }
            }
            catch (Exception ex)
            {
                PageBase.MessageBoxShow("操作失败，原因：" + ex.Message.ToString(), Page);
            }
        }

        private void BindRoleData()//绑定部门的所有信息
        {
            roles = new BRoles();
            this.RoleRepeater.DataSource = roles.SelectRoles();
            this.RoleRepeater.DataBind();
        }

        /// <summary>
        /// 修改保存角色信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                string id = this.hdfHiddenId.Value;
                roles = new BRoles();
                roleInfo = new Mtb_Roles();
                roleInfo.OID = Convert.ToInt32(id);
                bool result = roles.UpdateRoles(roleInfo);
                if (!result)
                {
                    PageBase.MessageBoxShow("抱歉，更新部门失败!", Page);
                    return;
                }
                else
                {
                    PageBase.MessageBoxShow("更新部门成功！", Page);
                }
            }
            catch (Exception ex)
            {
                PageBase.MessageBoxShow("操作失败，原因：" + ex.Message.ToString(), Page);
            }
        }

        /// <summary>
        /// 添加角色信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                roles = new BRoles();
                roleInfo = new Mtb_Roles();
                roleInfo.RoleName = txtName.Text.Trim();
                bool result = roles.InsertRoles(roleInfo);
                if (!result)
                {
                    PageBase.MessageBoxShow("抱歉，新增部门失败!", Page);
                    return;
                }
                else
                {
                    PageBase.MessageBoxShow("新增部门成功！", Page);
                }
            }
            catch (Exception ex)
            {
                PageBase.MessageBoxShow("操作失败，原因：" + ex.Message.ToString(), Page);
            }
        }

        /// <summary>
        /// 删除角色信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                string id = this.hdfHiddenId.Value;
                roles = new BRoles();
                roleInfo = new Mtb_Roles();
                roleInfo.OID = Convert.ToInt32(id);
                bool result = roles.DeleteRoles(roleInfo);
                if (!result)
                {
                    PageBase.MessageBoxShow("抱歉，删除失败!", Page);
                    return;
                }
                else
                {
                    PageBase.MessageBoxShow("删除成功！", Page);
                }
            }
            catch (Exception ex)
            {
                PageBase.MessageBoxShow("操作失败，原因：" + ex.Message.ToString(), Page);
            }
        }
    }
}